<?php
/**
 * Created by IntelliJ IDEA.
 * User: Kmontyjack
 * Date: 16.11.11
 * Time: 14:24
 * To change this template use File | Settings | File Templates.
 */
 
 class Knihovna_Model_SearchAutor
{
    /**
     * @var Zend_Search_Lucene_Interface
     */
    protected $index, $analyzer, $id_autor;


    public function create_index_autor()
    {

        $this->index = Zend_Search_Lucene::create('../application/var/search/autor');

        $this->analyzer = new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive();

    }

    public function __construct()
    {
    }

    /**
     *
     * Vezme autora a oindexuje si složky, podle kterých poté vyhledává
     * @param $data_autor array ve kterém je uloženo jmeno a prijmeni autora
     *
     * @return void
     */
    public function pridejAutora($data)
    {
        $this->index = Zend_Search_Lucene::open('../application/var/search/autor');

        $autor = new Zend_Search_Lucene_Document();

        $autor->addField(Zend_Search_Lucene_Field::Text('Jmeno', $data["jmeno"], 'UTF-8'));

        $autor->addField(Zend_Search_Lucene_Field::Text('Prijmeni', $data["prijmeni"], 'UTF-8'));

        $autor->addField(Zend_Search_Lucene_Field::Text('Narozeni', $data["narozeni"], 'UTF-8'));

        $autor->addField(Zend_Search_Lucene_Field::Binary('id_autor', $data["rid"]));

        $this->index->addDocument($autor);

        $this->index->commit();

        $this->id_autor++;
    }

     public function Indexuj_stavajici()
     {
        $this->index = Zend_Search_Lucene::open('../application/var/search/autor');

        $autortab = new Knihovna_Model_DbTable_Autor();

        $this->view->autori = $autortab->FetchAll();

        if                           //pokud nenalezne databázový index ve vyhl. indexu, pak přidává
        {
            $autor = new Zend_Search_Lucene_Document();

            $autor->addField(Zend_Search_Lucene_Field::Text('Jmeno', $data["jmeno"], 'UTF-8'));

            $autor->addField(Zend_Search_Lucene_Field::Text('Prijmeni', $data["prijmeni"], 'UTF-8'));

            $autor->addField(Zend_Search_Lucene_Field::Binary('Narozeni', $data["narozeni"], 'UTF-8'));

            $autor->addField(Zend_Search_Lucene_Field::Binary('id_autor', $data["rid"]));

            $this->index->addDocument($autor);

            $this->index->commit();

            $this->id_autor++;
        }
     }

     /**
     * Po delší době je dobré optimalizovat indexy
     *
     * @return void
     */
    Public function Optimalizuj_autora()
    {
        $this->index = Zend_Search_Lucene::open('../application/var/search/autor');

        $this->index->optimize();
    }

     /**
     *
     * Vyhledává autora naráz podle příjmení i jména
     * @param $dotaz_autor
     *
     * @return $hits
     */

    public function Hledej_autora($dotaz_autor)
    {
        $this->index = Zend_Search_Lucene::open('../application/var/search/autor');

        $this->query = new Zend_Search_Lucene_Search_Query_MultiTerm();

        $this->query->addTerm(new Zend_Search_Lucene_Index_Term($dotaz_autor, 'Jmeno'));

        $this->query->addTerm(new Zend_Search_Lucene_Index_Term($dotaz_autor, 'Prijmeni'));

        $hits  = $this->index->find($this->query);                                //hits je to, co našel

        return $hits;
    }

     /**
    public function Smaz_autora()
    {
        $this->index = Zend_Search_Lucene::open('../application/var/search/autor');

        Zend_Search_Lucene_Analysis_Analyzer::setDefault(
            new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive()
            );

        $hits = $this->index->find('');
        foreach ($hits as $hit)
        {
            $this->index->delete($hit->id)
        }

    }
      */

}